package cn.tedu.tmall.front.mall.service.impl;

import cn.tedu.tmall.common.consts.data.MallConsts;
import cn.tedu.tmall.common.enumerator.ServiceCode;
import cn.tedu.tmall.common.ex.ServiceException;
import cn.tedu.tmall.common.pojo.authentication.CurrentPrincipal;
import cn.tedu.tmall.common.pojo.vo.PageData;
import cn.tedu.tmall.front.mall.dao.persist.repository.ICommentRepository;
import cn.tedu.tmall.front.mall.dao.persist.repository.IGoodsRepository;
import cn.tedu.tmall.front.mall.pojo.entity.Comment;
import cn.tedu.tmall.front.mall.pojo.entity.Goods;
import cn.tedu.tmall.front.mall.pojo.param.CommentAddNewParam;
import cn.tedu.tmall.front.mall.pojo.vo.CommentListItemVO;
import cn.tedu.tmall.front.mall.pojo.vo.GoodsStandardVO;
import cn.tedu.tmall.front.mall.service.ICommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * 处理评论数据的业务实现类
 *
 * @author java@tedu.cn
 * @version 3.0
 */
@Slf4j
@Service
public class CommentServiceImpl implements ICommentService {

    @Value("${tmall.dao.default-query-page-size}")
    private Integer defaultQueryPageSize;
    @Autowired
    private ICommentRepository commentRepository;
    @Autowired
    private IGoodsRepository goodsRepository;

    public CommentServiceImpl() {
        log.debug("创建业务类对象：CommentServiceImpl");
    }

    @Override
    public void addNew(CurrentPrincipal currentPrincipal, CommentAddNewParam commentAddNewParam) {
        log.debug("开始处理【发表评论】的业务，参数：{}", commentAddNewParam);

        Long goodsId = commentAddNewParam.getGoodsId();
        GoodsStandardVO currentGoods = goodsRepository.getStandardById(goodsId);
        if (currentGoods == null) {
            String message = "发表评论失败，商品数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        if (currentGoods.getIsPutOn() == PUT_ON_STATE_OFF) {
            String message = "发表评论失败，此商品已经下架！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        // 另需检查是否购买过此商品
        // 另需检查是否已经评论过此商品

        Comment comment = new Comment();
        BeanUtils.copyProperties(commentAddNewParam, comment);
        comment.setAuthorId(currentPrincipal.getId());
        comment.setAuthorName(currentPrincipal.getUsername());
        comment.setCheckState(CHECK_STATE_UNCHECKED);
        int rows = commentRepository.insert(comment);
        if (rows != 1) {
            String message = "发表评论失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }

        Goods updateGoods = new Goods();
        updateGoods.setId(goodsId);
        updateGoods.setCommentCount(currentGoods.getCommentCount() + 1);
        switch (commentAddNewParam.getType()) {
            case COMMENT_TYPE_POSITIVE:
                updateGoods.setPositiveCommentCount(updateGoods.getPositiveCommentCount() + 1);
                break;
            case COMMENT_TYPE_NEGATIVE:
                updateGoods.setNegativeCommentCount(updateGoods.getNegativeCommentCount() + 1);
                break;
        }
        rows = goodsRepository.updateById(updateGoods);
        if (rows != 1) {
            String message = "发表评论失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

    @Override
    public PageData<CommentListItemVO> list(Long goodsId, Integer pageNum) {
        log.debug("开始处理【查询商品的评论列表】的业务，页码：{}", pageNum);
        return commentRepository.list(goodsId, pageNum, defaultQueryPageSize);
    }

    @Override
    public PageData<CommentListItemVO> list(Long goodsId, Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询商品的评论列表】的业务，页码：{}，每页记录数：{}", pageNum, pageSize);
        return commentRepository.list(goodsId, pageNum, pageSize);
    }

}
